/*
 * SonarLint for IntelliJ IDEA
 * Copyright (C) 2015-2024 SonarSource
 * sonarlint@sonarsource.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonarlint.intellij.finding.issue.vulnerabilities

import com.intellij.openapi.module.Module
import java.time.Instant
import java.util.Collections
import java.util.UUID
import org.sonarlint.intellij.finding.Flow
import org.sonarlint.intellij.finding.Issue
import org.sonarlint.intellij.finding.Location
import org.sonarlint.intellij.finding.issue.LiveIssue
import org.sonarsource.sonarlint.core.client.utils.CleanCodeAttribute
import org.sonarsource.sonarlint.core.client.utils.ImpactSeverity
import org.sonarsource.sonarlint.core.client.utils.SoftwareQuality
import org.sonarsource.sonarlint.core.rpc.protocol.backend.rules.ImpactDto
import org.sonarsource.sonarlint.core.rpc.protocol.backend.tracking.TaintVulnerabilityDto
import org.sonarsource.sonarlint.core.rpc.protocol.common.IssueSeverity
import org.sonarsource.sonarlint.core.rpc.protocol.common.RuleType

class LocalTaintVulnerability(val module: Module?, private val primaryLocation: Location,
                              val flows: List<Flow>, private val remoteTaintVulnerability: TaintVulnerabilityDto, private var resolved: Boolean): Issue {

    private val highestQuality =
        SoftwareQuality.fromDto(Collections.max(getImpacts(), Comparator.comparing(ImpactDto::getImpactSeverity)).softwareQuality)
    private val highestImpact =
        ImpactSeverity.fromDto(Collections.max(getImpacts(), Comparator.comparing(ImpactDto::getImpactSeverity)).impactSeverity)

    fun rangeMarker() = primaryLocation.range
    override fun file() = primaryLocation.file
    fun key(): String = remoteTaintVulnerability.sonarServerKey
    fun message(): String = remoteTaintVulnerability.message ?: ""
    fun creationDate(): Instant = remoteTaintVulnerability.introductionDate
    fun severity(): IssueSeverity? =
        if (remoteTaintVulnerability.severityMode.isLeft) remoteTaintVulnerability.severityMode.left.severity else null
    override fun getId(): UUID = remoteTaintVulnerability.id
    override fun resolve() {
        resolved = true
    }

    override fun reopen() {
        resolved = false
    }

    override fun getCleanCodeAttribute(): CleanCodeAttribute? =
        if (remoteTaintVulnerability.severityMode.isRight) CleanCodeAttribute.fromDto(remoteTaintVulnerability.severityMode.right.cleanCodeAttribute) else null

    override fun getImpacts(): List<ImpactDto> =
        if (remoteTaintVulnerability.severityMode.isRight) remoteTaintVulnerability.severityMode.right.impacts else emptyList()
    override fun getHighestQuality(): SoftwareQuality? = highestQuality
    override fun getHighestImpact(): ImpactSeverity? = highestImpact

    override fun isResolved() = resolved
    override fun getServerKey() = key()
    override fun getRuleKey(): String = remoteTaintVulnerability.ruleKey
    override fun getType(): RuleType? =
        if (remoteTaintVulnerability.severityMode.isLeft) remoteTaintVulnerability.severityMode.left.type else null
    override fun isValid() = file()?.isValid == true && rangeMarker()?.isValid == true
    override fun isOnNewCode() = remoteTaintVulnerability.isOnNewCode

    fun getValidTextRange() = LiveIssue.toValidTextRange(rangeMarker())
    override fun getRuleDescriptionContextKey() = remoteTaintVulnerability.ruleDescriptionContextKey

}
